﻿#pragma once
#include<iostream>
using namespace std;
//二叉搜索树
//1，若它的左⼦树不为空，则左⼦树上所有结点的值都⼩于等于根结点的值
//2，若它的右⼦树不为空，则右⼦树上所有结点的值都⼤于等于根结点的值
//3，它的左右⼦树也分别为⼆叉搜索树

namespace key
{
	template<class K>
	struct BSTNode
	{
		K _key;
		BSTNode<K>* _left;
		BSTNode<K>* _right;
		BSTNode(const K& key)
			:_key(key)
			, _left(nullptr)
			, _right(nullptr)
		{
		}
	};

	//Binary Search Tree 二叉搜索树
	//Key
	template <class K>
	class BSTree
	{
		typedef BSTNode<K> Node;//也可以使用 using Node=BSTNode;

	public:
		//搜索二叉树的插入
		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				//一版：相等不允许插入，去重
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}
			return true;
		}

		//二叉搜索树的查找
		bool Find(const K& key)
		{

			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}
			return false;
		}

		//二叉搜索树的删除
		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					//开始删除

					//左为空
					if (cur->_left == nullptr)
					{
						//先判断要删除的是否是根节点
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							//判断在父节点的哪边
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
					}
					else if (cur->_right == nullptr)//右为空
					{
						//先判断要删除的是否是根节点
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
					}
					else
					{
						//左右都不为空，找到替代节点replace
						//右子树的最左节点或者左子树的最右节点
						//右子树的最左节点比左子树都大，比右子树都小
						Node* replaceParent = cur;
						Node* replace = cur->_right;
						while (replace->_left)
						{
							replaceParent = replace;
							replace = replace->_left;
						}
						//替换值后删除多余节点
						cur->_key = replace->_key;
						if (replaceParent->_left == replace)
							replaceParent->_left = replace->_right;
						else
							replaceParent->_right = replace->_right;
						delete replace;
					}
					return true;
				}
			}
			return false;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		//中序遍历
		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}

	private:
		Node* _root = nullptr;
	};
}





//第二种结构
namespace key_value
{
	template<class K,class V>
	struct BSTNode
	{
		K _key;
		V _value;
		BSTNode<K,V>* _left;
		BSTNode<K,V>* _right;
		BSTNode(const K& key,const V& value)
			:_key(key)
			,_value(value)
			, _left(nullptr)
			, _right(nullptr)
		{
		}
	};

	//Binary Search Tree 二叉搜索树
	//Key
	template <class K,class V>
	class BSTree
	{
		typedef BSTNode<K,V> Node;//也可以使用 using Node=BSTNode;

	public:
		//强制生成构造，C++11提供
		BSTree() = default;
		//拷贝构造
		BSTree(const BSTree& t)
		{
			_root = Copy(t._root);
		}
		//赋值
		BSTree& operator=(BSTree tmp)
		{
			swap(_root, tmp._root);
			return *this;
		}
		//析构
		~BSTree()
		{
				Destroy(_root);
				_root = nullptr;	
		}
		

		//搜索二叉树的插入
		bool Insert(const K& key,const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key,value);
				return true;
			}

			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				//一版：相等不允许插入，去重
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key,value);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}
			return true;
		}

		//二叉搜索树的查找，key_value结构只需找到对应的key，返回对应节点以方便对value值进行修改
		Node* Find(const K& key)
		{

			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}
			return nullptr;
		}

		//二叉搜索树的删除，key_value结构只需找到对应的key
		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					//开始删除

					//左为空
					if (cur->_left == nullptr)
					{
						//先判断要删除的是否是根节点
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							//判断在父节点的哪边
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
					}
					else if (cur->_right == nullptr)//右为空
					{
						//先判断要删除的是否是根节点
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
					}
					else
					{
						//左右都不为空，找到替代节点replace
						//右子树的最左节点或者左子树的最右节点
						//右子树的最左节点比左子树都大，比右子树都小
						Node* replaceParent = cur;
						Node* replace = cur->_right;
						while (replace->_left)
						{
							replaceParent = replace;
							replace = replace->_left;
						}
						//替换值后删除多余节点
						cur->_key = replace->_key;
						if (replaceParent->_left == replace)
							replaceParent->_left = replace->_right;
						else
							replaceParent->_right = replace->_right;
						delete replace;
					}
					return true;
				}
			}
			return false;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		//中序遍历
		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << ":" << root->_value << endl;;
			_InOrder(root->_right);
		}

		//后序析构
		void Destroy(Node* root)
		{
			if (root == nullptr)
				return;
			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
		}

		//前序遍历进行深拷贝
		Node* Copy(Node* root)
		{
			if (root == nullptr)
				return nullptr;
			Node* newRoot = new Node(root->_key, root->_value);
			newRoot->_left = Copy(root->_left);
			newRoot->_right = Copy(root->_right);
			return newRoot;
		}

	private:
		Node* _root = nullptr;
	};
}